using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using Peak.Can.Basic;

namespace WinCANTool
{
    public partial class SendSDO : Form
    {

        public byte[] MyText = { 0x42, 0x18, 0x10, 0x01, 0, 0, 0, 0 };
        public NewMessage CurrentMessage;
        private byte m_hClient = 0;
        private byte m_hNet = 0;
        private byte m_hHw = 0;
        private new MDIWindow ParentForm;
        private static SendSDO aForm = null;

        public event NewMessageHandler NewData;


        public static SendSDO MyInstance(MDIWindow Parent)
        {
            if (aForm == null)
            {
                aForm = new SendSDO(Parent);
            }
            
            return aForm;

        }

        protected virtual void MessageReady(NewEventArgs e)
        {

            if (NewData != null)
                NewData(this, e);
        }

        private SendSDO(MDIWindow Parent)
        {
            InitializeComponent();

            CurrentMessage = new NewMessage();
            
            CurrentMessage.SaveSendRate(10);
            ParentForm = Parent;

            for (int i = 0; i < 8; i++)
            {
                //if (MyText[i] != null)
                {
                    CurrentMessage.SaveDataInt(i, MyText[i]);
                }
            }


        }

        private void SendMessage(object sender, EventArgs e)
        {
        	            TPCANStatus stsResult;

            uint identval = (uint) Int32.Parse(textBox1.Text, NumberStyles.Integer);
            identval += 0x600;
            CurrentMessage.SaveID_DLC_Int(identval, 8);

            //SendCanMessage(ref CurrentMessage);


            TPCANMsg CANMsgToSend = new TPCANMsg();
            CANMsgToSend = CurrentMessage.GetCANMsg();
            m_hClient = ParentForm.GetClientHandle();
            m_hNet = ParentForm.GetNetHandle();
            m_hHw = ParentForm.GetHwHandle();
            //TPCANTimestamp tstamp = new TPCANTimestamp();

            // Get system time
            //CanApi2.GetSystemTime(out tstamp);
            //CurrentMessage.SaveTimeStamp(tstamp);

            // Send message
            stsResult = PCANBasic.Write(m_hClient, ref CANMsgToSend);

            
            //NewEventArgs NewArgsMessage = new NewEventArgs();
            //NewArgsMessage.EventMessage = CurrentMessage;
            //MessageReady(NewArgsMessage);


        }






    }
}
